package com.zoho.daliansheng.exception;

import com.zoho.daliansheng.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * -------- 通用异常处理方法 --------
     **/
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultUtil error(Exception e) {
        e.printStackTrace();
        log.error("GlobalExceptionHandler={}", e.getMessage());
        return ResultUtil.error(e.getMessage());
    }

    /**
     * -------- 指定异常处理方法 --------
     **/
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public void error(IllegalArgumentException e) {
        log.error(e.getMessage());
    }

    /**
     * -------- 指定异常处理方法 --------
     **/
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public void error(IOException e) {
        log.error(e.getMessage());
    }
}
